package com.whity.towerdefense;

import org.xmlpull.v1.XmlPullParser;

import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.model.Map;
import com.whity.towerdefense.view.GraphicElt;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.content.SharedPreferences;
import android.graphics.PointF;
import android.util.Log;

public class Utils
{
	// =============================================================
	// Public defines
	
	// ID for Google analytics :
	public static final String ID_GOOGLE_ANALYTICS 	= "UA-22633480-3";
	
	// Path relative to the assets folder where we can find map XML descriptor
	public static final String MAP_PATH 			= "Gameplay/Maps";
	public static final String UNIT_PATH 			= "Gameplay/Units";
	public static final String TOWER_PATH			= "Gameplay/Towers";
	public static final String PANIC_IMAGE			= "Graphic/error.png";
	public static final String GRAPHICS_PATH 		= "Graphic/";
	
	// Variable for debugging
	//public static final boolean	DEBUG = true;
	
	// Shared preference for user save.
	public static SharedPreferences BACKEDUP_DATA;
	public static SharedPreferences LOCAL_DATA;
	
	// Error code :
	public static final int		CANT_LOAD_PANIC_IMAGE			= -4;
	public static final	int		MAP_PARSING_FATAL_ERROR 		= -6;
	public static final	int		UNIT_TYPE_PARSING_FATAL_ERROR 	= -7;
	public static final int		TOWER_TYPE_PARSING_FATAL_ERROR 	= -8;
	
	// =============================================================
	// Public attributs :
	
	public static int					 RESOLUTION_X = -1;
	public static int					 RESOLUTION_Y = -1;
	public static float					 SCREEN_RATIO = -1;
	public static GoogleAnalyticsTracker TRACKER;
	
	// =============================================================
	// Public functions :
	
	/**
	 * Change coordinate from screen to map repere.
	 * @param p_pt the point to modify
	 */
	public static void ScreenToMap(PointF p_Pt)
	{
		GraphicElt mapGraph = GameModel.LEVEL.GetMap().GetGraphic(); 
		PointF mapPos = GameModel.LEVEL.GetMap().GetPosition();
		float mapScale = mapGraph.GetScale();
		
		p_Pt.y = Utils.RESOLUTION_Y - p_Pt.y;
		
		// change to 0->1 for Y coordinate, and 0->ScreenRatio for X coordinate
		p_Pt.x /= Utils.RESOLUTION_Y;
		p_Pt.y /= Utils.RESOLUTION_Y;
		
		
		// Substract map position without taking care of map scale
		p_Pt.x -= mapPos.x;
		p_Pt.y -= mapPos.y;
		
		
		// Take care of the map scale
		p_Pt.x = p_Pt.x/mapScale;
		p_Pt.y = p_Pt.y/mapScale;
		p_Pt.x /= Map.UNIT_SIZE;
		p_Pt.y /= Map.UNIT_SIZE;
	}
	
	/**
	 * Change coordinate from Map to Screen space.
	 * @param p_pt the point to modify
	 */
	public static void MapToScreen(PointF p_Pt)
	{
		GraphicElt mapGraph = GameModel.LEVEL.GetMap().GetGraphic(); 
		PointF mapPos = mapGraph.GetPosition();
		float mapScale = mapGraph.GetScale();
		
		// Take care of the map scale
		p_Pt.x = p_Pt.x/mapScale*SCREEN_RATIO;
		p_Pt.y = p_Pt.y/mapScale;
				
		// Add map position without taking care of map scale
		p_Pt.x += mapPos.x;
		p_Pt.y += mapPos.y;
		// change to 0->1 for Y coordinate, and 0->ScreenRatio for X coordinate
		p_Pt.x *= Utils.RESOLUTION_X;
		p_Pt.y *= Utils.RESOLUTION_Y;
	}
	
	public static Float ParseFloat(XmlPullParser p_XmlParser, String p_AttrName)
	{
		String attr = p_XmlParser.getAttributeValue(null, p_AttrName);
		if(attr != null)
		{
			return Float.parseFloat(attr);
		}
		else
		{
			return null;
		}
		
	}
	
	// =============================================================
}
